package site.shader85.Kingdoms.war;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;

import site.shader85.Kingdoms.KingdomsPlugin;

public class WarConfig extends YamlConfiguration {

	private KingdomsPlugin plugin;
	private File f;

	public WarConfig(KingdomsPlugin plugin) {
		this.plugin = plugin;
		this.f = new File(this.plugin.getDataFolder(), "war.yml");
		plugin.saveResource("war.yml", false);
		try {
			load(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidConfigurationException e) {
			e.printStackTrace();
		}
	}

	public void save() {
		try {
			save(f);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public WarTime getWarTime() {
		String time = getString("begin");
		String[] s = time.split(":");
		Clock bg = new Clock(Integer.parseInt(s[0]), Integer.parseInt(s[1]));
		time = getString("end");
		s = time.split(":");
		Clock ed = new Clock(Integer.parseInt(s[0]), Integer.parseInt(s[1]));
		return new WarTime(bg, ed);
	}

	public static class Clock {
		public final int hour;
		public final int minute;

		public Clock(int hour, int minute) {
			this.hour = hour;
			this.minute = minute;
		}

		public static Clock addTenMinute(Clock c) {
			int hour = c.hour;
			int min = c.minute;
			if (min < 50) {
				min += 10;
				return new Clock(hour, min);
			}
			hour++;
			min += 10 - 60;
			return new Clock(hour, min);
		}

		public boolean after(Clock c) {
			if (this.hour > c.hour)
				return true;
			if (this.hour < c.hour)
				return false;
			if (this.minute > c.minute)
				return true;
			if (this.minute < c.minute)
				return false;
			return true;
		}

		@SuppressWarnings("deprecation")
		public static Clock getByDate(Date d) {
			int hour = d.getHours();
			int min = d.getMinutes();
			return new Clock(hour, min);
		}

		public boolean before(Clock c) {
			if (this.hour > c.hour)
				return false;
			if (this.hour < c.hour)
				return true;
			if (this.minute > c.minute)
				return false;
			if (this.minute < c.minute)
				return true;
			return true;
		}
	}

	public static class WarTime {
		public final Clock begin;
		public final Clock end;

		private WarTime(Clock begin, Clock end) {
			this.begin = begin;
			this.end = end;
		}

		public boolean inTime(Clock other) {
			return other.after(begin) && other.before(end);
		}

	}
}
